package com.andriod.waker;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MainListAdapter extends BaseAdapter {
	
	private LayoutInflater mInflater;
	private HostDBOpenHelper helper;
	private SQLiteDatabase DB;
	private Cursor cursor;
	
	public MainListAdapter(Context context)
	{
		mInflater = LayoutInflater.from(context);
		helper = HostDBOpenHelper.Get_instance(context, 1);
		DB = helper.getReadableDatabase();
		
		if(DB == null)
		{
			Log.d("DB_ERROR", "DB == null");
			return;
		}
		cursor = DB.query(HostDBOpenHelper.HOST_TABLE, null, null, null, null, null, null);
	}

	public int getCount() {
		// TODO Auto-generated method stub
		cursor = DB.query(HostDBOpenHelper.HOST_TABLE, null, null, null, null, null, null);
		return cursor.getCount();
	}

	public Object getItem(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		cursor = DB.query(HostDBOpenHelper.HOST_TABLE, null, null, null, null, null, null);
		
		Inner_holder holder = new Inner_holder();
		if(convertView == null)
		{
			convertView = mInflater.inflate(R.layout.list_item, null);
			holder.iv = (ImageView)convertView.findViewById(R.id.vimageView1);
			holder.host = (TextView)convertView.findViewById(R.id.vhost_name);
			holder.mac = (TextView)convertView.findViewById(R.id.vmac_domain);
			
			convertView.setTag(holder);
		}
		else
		{
			holder = (Inner_holder)convertView.getTag();
		}
		
		if(DB != null)
		{
			Log.d("MOVE_TO", String.valueOf(cursor.getCount() - position - 1));
			cursor.moveToPosition(cursor.getCount() - position - 1);
			holder.host.setText(cursor.getString(cursor.getColumnIndex("NAME")));
			holder.mac.setText(cursor.getString(cursor.getColumnIndex("MAC")));
			holder.id = Integer.parseInt(cursor.getString(cursor.getColumnIndex("_ID")));
			holder.domain_string = cursor.getString(cursor.getColumnIndex("DOMAIN_NAME"));
			holder.port_string = cursor.getString(cursor.getColumnIndex("PORT_NUM"));
		}
		
		return convertView;
	}
	
	/////////////////////////////////////////////
	public void Drop_table(String table_name)
	{
		DB = helper.getWritableDatabase();
		DB.execSQL("DROP TABLE " + table_name);
		/*DB = helper.getWritableDatabase();
		if(cursor.getCount() == 1)
		{
			return;
		}
		cursor.moveToPosition(0);
		while(cursor.moveToNext())
		{
			Log.d("_ID", cursor.getString(cursor.getColumnIndex("_ID")));
			int temp = Integer.parseInt(cursor.getString(cursor.getColumnIndex("_ID")));
			DB.delete(table_name, "_ID=?", new String[]{temp + ""});
		}
		this.notifyDataSetChanged();*/
	}
	
	public void DeleteItem(int id,String mac)
	{
		DB = helper.getWritableDatabase();
		DB.delete(HostDBOpenHelper.HOST_TABLE, "_ID=?", new String[]{id + ""});
		this.notifyDataSetChanged();
	}
	
	public class Inner_holder
	{
		ImageView iv;
		TextView host;
		TextView mac;
		int id;
		String domain_string;
		String port_string;
	}
}
